﻿using System;
using System.Collections.Generic;
using System.Linq;

using MvcCms.Service.Cache;
using MvcCmsProjectTracker.Data;
using MvcCms.Data;
using MvcCms.Service.Logging;
using MvcCms.Service.Code;
using MvcCms.Service.Code.DataTables;
using Microsoft.Practices.ServiceLocation;
using MvcCms.Service;
using MvcCms.Service.Models;
using MvcCms.Service.Code.Email;

namespace MvcCmsProjectTracker.Service
{
    public class TicketService : ITicketService
    {
        #region Constructors

        private readonly ITicketRepository _repository;
        private readonly ICacheService _cacheService;
        public TicketService(ITicketRepository repository, ICacheService cacheService)
        {
            _repository = repository;
            _cacheService = cacheService;
        }

        #endregion
        public void Save()
        {
            try
            {
                _repository.Save();
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
            }
        }
        public bool CreateTicket(Ticket ticketToCreate)
        {            
            try
            {
                _repository.CreateTicket(ticketToCreate);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool EditTicket(Ticket ticketToEdit)
        {
            // Database logic
            try
            {
                _repository.EditTicket(ticketToEdit);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        #region List Methods
        public IQueryable<TicketAttachment> GetTicketAttachments(int ticketID)
        {
            return _repository.GetTicketAttachments(ticketID);
        }
        public IQueryable<TicketComment> GetTicketComments(int ticketID)
        {
            return _repository.GetTicketComments(ticketID);
        }
        public IQueryable<TicketHistory> GetTicketHistories(int ticketID)
        {
            return _repository.GetTicketHistories(ticketID);
        }
        public IQueryable<TicketNotification> GetTicketNotifications(int ticketID)
        {
            return _repository.GetTicketNotifications(ticketID);
        }
        public IQueryable<Ticket> GetTicketsByPriority(int projectID, int priorityID)
        {
            return _repository.GetTicketsByPriority(projectID, priorityID);
        }
        public IQueryable<Ticket> GetTicketsByProject(int projectID)
        {
            return _repository.GetTicketsByProject(projectID);
        }
        public IQueryable<Ticket> GetTicketsByStatus(int projectID, int statusID)
        {
            return _repository.GetTicketsByStatus(projectID, statusID);
        }
        public IQueryable<Ticket> GetOpenHelpDeskTickets()
        {
            return _repository.GetOpenHelpDeskTickets(_cacheService.GetCurrentPortal().PortalID);
        }        
        public IQueryable<Ticket> GetMyTickets(Guid userId)
        {
            return _repository.GetMyTickets(userId);
        }
        #endregion

        #region Get Methods
        public Ticket GetTicket(int ticketID)
        {
            return _repository.GetTicket(ticketID);
        }
        public TicketAttachment GetTicketAttachment(int attachmentID)
        {
            return _repository.GetTicketAttachment(attachmentID);
        }
        public TicketComment GetTicketComment(int commentID)
        {
            return _repository.GetTicketComment(commentID);
        }
        public TicketHistory GetTicketHistory(int historyID)
        {
            return _repository.GetTicketHistory(historyID);
        }
        public TicketNotification GetTicketNotification(int notificationID)
        {
            return _repository.GetTicketNotification(notificationID);
        }

        #endregion

        #region Add Methods
        public bool AddTicket(Ticket ticket)
        {
            try
            {
                _repository.AddTicket(ticket);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddTicketAttachment(TicketAttachment attachment)
        {
            try
            {
                _repository.AddTicketAttachment(attachment);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddTicketComment(TicketComment comment)
        {
            try
            {
                _repository.AddTicketComment(comment);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddTicketHistory(TicketHistory history)
        {
            try
            {
                _repository.AddTicketHistory(history);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool AddTicketNotification(TicketNotification notification)
        {
            try
            {
                _repository.AddTicketNotification(notification);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }

        #endregion

        #region Delete Methods
        public bool DeleteTicket(Ticket ticketToDelete)
        {
            try
            {
                _repository.DeleteTicket(ticketToDelete);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteTicketAttachment(TicketAttachment attachment)
        {
            try
            {
                _repository.DeleteTicketAttachment(attachment);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteTicketComment(TicketComment comment)
        {
            try
            {
                _repository.DeleteTicketComment(comment);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteTicketHistory(TicketHistory history)
        {
            try
            {
                _repository.DeleteTicketHistory(history);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }
        public bool DeleteTicketNotification(TicketNotification notification)
        {
            try
            {
                _repository.DeleteTicketNotification(notification);
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }

        #endregion

    }
}